﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：HttpWebClientHelper
// 创 建 者：杨程
// 创建时间：2022/2/23 14:28:39
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Vampirewal.Core.YuQueHelper
{
    internal static class HttpWebClientHelper
    {
        /// <summary>
        /// get请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="token">语雀个人设置的token</param>
        /// <param name="userAgent">语雀需要收集的用户标识</param>
        /// <returns>请求到的数据</returns>
        public static string HttpGet(string url, string token, string userAgent = "netCoreSdk")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Headers.Add("User-Agent", HttpUtility.UrlEncode(userAgent));
            request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode(token));

            using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            return reader.ReadToEnd();
        }

        /// <summary>
        /// get请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="token">语雀个人设置的token</param>
        /// <param name="userAgent">语雀需要收集的用户标识</param>
        /// <returns>请求到的数据</returns>
        public static async Task<string> HttpGetAsync(string url, string token, string userAgent = "netCoreSdk")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Headers.Add("User-Agent", HttpUtility.UrlEncode(userAgent));
            request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode(token));

            using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            return await reader.ReadToEndAsync();
        }

        public static string HttpPost(string data,string url, string token, string userAgent = "netCoreSdk")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";

            //string retval = null;


            request.Headers.Add("User-Agent", HttpUtility.UrlEncode(userAgent));
            request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode(token));
            request.Headers.Add("Content-Type", "application/json");

            var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(data);

            using (var stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }

            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取服务器返回的结果
            //Stream getStream = response.GetResponseStream();
            //StreamReader streamreader = new StreamReader(getStream);
            //String result = streamreader.ReadToEnd();

            //return result;


            //using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //using StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        }
    }
}
